package com.example.healkep;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.healkep.MainActivity.MainActivityGetinFood;
import com.example.healkep.bean.globalUserInfo;
import com.example.healkep.bean.insulin;
import com.example.healkep.datepicker.CustomDatePicker;
import com.example.healkep.datepicker.DateFormatUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class InsulinActivity extends AppCompatActivity {
    private TextView datetime;
    private EditText yds;
    private Button submit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insulin);
        initComponent();
    }
    private void initComponent(){
        datetime = findViewById(R.id.tv_datetime);
        String beginTime = "2018-00-00 00:00";
        String endTime = DateFormatUtils.long2Str(System.currentTimeMillis(), true);
        datetime.setText(endTime);
        datetime.setOnClickListener((view)->{
            new CustomDatePicker(InsulinActivity.this, new CustomDatePicker.Callback() {
                @Override
                public void onTimeSelected(long timestamp) {
                    datetime.setText(DateFormatUtils.long2Str(timestamp, true));
                }
            }, beginTime, endTime).show(endTime);
        });
        yds = findViewById(R.id.et_yds);
        submit = findViewById(R.id.btn_submit);
        submit.setOnClickListener((view)->{
            final insulin in = new insulin();
            in.setUsername(globalUserInfo.getInstance().getUsername());
            in.setDatetime(datetime.getText().toString());
            double volume = 0;
            if(yds.getText().length()>0){
                volume = Double.parseDouble(yds.getText().toString());
            }
            in.setVolume(volume);
            new Thread(()->{
                OkHttpClient client = new OkHttpClient();
                Gson gson = new Gson();
                String json = gson.toJson(in);
                MediaType type = MediaType.parse("application/json");
                RequestBody requestBody = RequestBody.create(json,type);
                Request request = new Request.Builder()
                        .url(LoginActivity.url+"/insulin/insert")
                        .post(requestBody)
                        .build();
                Call call = client.newCall(request);
                try {
                    Response response = call.execute();
                    if(response.code()!=200){
                        runOnUiThread(()->{
                            Toast.makeText(InsulinActivity.this,"网络异常",Toast.LENGTH_SHORT).show();
                        });
                        return;
                    }else{
                        String str = response.body().string();
                        JsonObject object = gson.fromJson(str,JsonObject.class);
                        if(object.has("code")){
                            if(object.get("code").getAsInt() == 1){
                                runOnUiThread(()->{
                                    Toast.makeText(InsulinActivity.this,"上传成功",Toast.LENGTH_SHORT).show();
                                });
                            }else{
                                runOnUiThread(()->{
                                    Toast.makeText(InsulinActivity.this,"上传失败",Toast.LENGTH_SHORT).show();
                                });
                            }
                        }else{
                            runOnUiThread(()->{
                                Toast.makeText(InsulinActivity.this,"网络异常",Toast.LENGTH_SHORT).show();
                            });
                        }
                    }
                } catch (IOException e) {
                    runOnUiThread(()->{
                        Toast.makeText(InsulinActivity.this,"网络异常",Toast.LENGTH_SHORT).show();
                    });
                    e.printStackTrace();
                }
            }).start();
        });
    }
}